https://introtcs.org/public/lec_02_representation.html
๊ฐ์
The main takeaways from this chapter are:
- We can represent all kinds of objects we want to use as inputs and outputs using binary strings. For example, we can use the binary basis to represent integers and rational numbers as binary strings (see Section 2.1.1 and Section 2.2).
- We can compose the representations of simple objects to represent more complex objects. In this way, we can represent lists of integers or rational numbers, and use that to represent objects such as matrices, images, and graphs. Prefix-free encoding is one way to achieve such a composition (see Section 2.5.2).
- A computational task specifies a map from an input to an outputโ a function. It is crucially important to distinguish between the โwhatโ and the โhowโ, or the specification and implementation (see Section 2.6.1). A function simply defines which output corresponds to which input. It does not specify how to compute the output from the input, and as weโve seen in the context of multiplication, there can be more than one way to compute the same function.
- While the set of all possible binary strings is infinite, it still cannot represent everything. In particular, there is no representation of the real numbers (with absolute accuracy) as binary strings. This result is also known as โCantorโs Theoremโ (see Section 2.4) and is typically referred to as the result that the โreals are uncountable.โ It is also implied that there are different levels of infinity though we will not get into this topic in this book (see Remark 2.10).
The two โbig ideasโ we discuss are Big Idea 1 - we can compose representations for simple objects to represent more complex objects and Big Idea 2 - it is crucial to distinguish between functionsโ (โwhatโ) and programsโ (โhowโ). The latter will be a theme we will come back to time and again in this book.
์ด๋ฒ ์ฅ์์๋ ์ด ๋ค ๊ฐ์ง์ ๋ํด์ ๋ฐฐ์ด๋ค๊ณ ํ๋ค.
- '๋ชจ๋ ์ข
๋ฅ'์ ์๋ฃ๋ฅผ binary strings("01110001110")๋ก ํํ์ด ๊ฐ๋ฅํ๋ค๋ ์ฌ์ค.
- prefix-free encoding์ด๋ผ๋ ๋ฐฉ๋ฒ์ ํตํด ๋ณต์กํ ์๋ฃ๋ฅผ ํฌํํ๋ค๋ ์ฌ์ค. (๋ณต์กํ๊ฑธ ์ด์ง๋ฒ์ผ๋ก ํํํ ์ ์๋ ๋ ๋จ์ํ ๊ฒ๋ค์ ๋ฌถ์์ผ๋ก ํํ)
- ์ปดํจํฐ๊ฐ ํ๋์ผ์ ํจ์๋ค. ์
๋ ฅ -> ์ถ๋ ฅ์ ๋งตํํ๋ ์ผ.
- ์ด์ง๋ฒ์ ์กฐํฉ์ ๋ฌดํํ์ง๋ง, ๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ ์ธ์์ ๋ชจ๋ ๊ฒ์ ํํํ ์๋ ์๋ค๋ ์ฌ์ค. (์นธํ ์ด์ ์ด๋ก )
์ด์ง์๋ก ํํํ๊ธฐ
"ํํ"
์ฐ๋ฆฌ๊ฐ ์ปดํจํฐ์ ์ฌ์ง, ์์
, ์์ ๋ฑ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋ ์ง์ง๋ก ์ ์ฅ๋๋ ๊ฒ์ ๊ทธ๊ฒ๋ค์ 'ํํ'์ด๋ค. ์ฐ๋ฆฌ์ ๋ ๋ง์ ๋ ๊ฐ๊ฐ๋ ๊ทธ ์์ฒด๊ฐ ์
๋ ฅ๋์ง ์๊ณ ๊ทธ๊ฒ์ ํํ์ ์ ์ฅํ๋ค.
์ปดํจํฐ๋ 0๊ณผ 1๋ก ๋ binary strings๋ฐ์ ์ ์ฅํ์ง ๋ชปํ๋๊น ๊ฒฐ๊ตญ ๋ชจ๋ ๊ฑธ 0๊ณผ 1๋ก ๋ฒ์ญํ๊ณ ๊ทธ๊ฑธ ์ ์ฅํ๋ค.
๊ทธ๋์ ์ค์ํ๊ฑด "์ด๋ป๊ฒ"์ ๋ฌธ์ ๋ค. representation scheme.
์์ฐ์, ์์, ๋ฌธ์์ด, ์ด๋ฏธ์ง, ์์ ๋ฑ๋ฑ ๋ชจ๋ ๊ฑธ ์ด๋ป๊ฒ 0๊ณผ1๋ก ๋ง๋ค๊ฒ์ธ๊ฐ?
์์ฐ์
์์ฐ์๋ฅผ ์ด์ง๋ฒ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ์ ๋งค์ฐ ์ต์ํ๋ค.
์ด๊ฑธ ํ์ด์ฌ์ผ๋ก ํํํ๋ฉด ์ด๋ ๋ค.
def NtS(n):# natural numbers to strings
if n > 1:
return NtS(n // 2) + str(n % 2)
else:
return str(n % 2)
print(NtS(236))
# 11101100
print(NtS(19))
# 10011
โํด๋น ์ซ์๋ฅผ 2๋ก ๋๋ ๋๋จธ์ง (0 or 1)์ ๊ธฐ์
.
๊ทธ๋ฆฌ๊ณ ๋ค์ ๊ทธ ์ซ์๋ฅผ 2๋ก ๋๋๋ค์ ๋ฐ๋ณต.
์ธ์ ๊น์ง? n์ด 2 ๋ฏธ๋ง์ผ ๋ ๊น์ง.
์์
์์ NtS()๊ฒฐ๊ณผ ์์ ์์๋ผ๋ฉด 0์ ์์๋ผ๋ฉด 1์ ์ถ๊ฐํ๋ค.
์ด๊ฑด ๊ณผ๊ฑฐ์ ๋ฐฉ๋ฒ์ด๊ณ ์์ฆ์ ๋ฐฉ๋ฒ์ ๋ฐ์์ ์์ .
์ด๋ป๊ฒ ๊ตฌ๋ณํ๋?
์์์๋ ๋ณด๋ฉด NtS(19)์ ๊ฒฐ๊ณผ๋ฌผ์ 10011์ธ๋ฐ, 19๋ ์์๊ฐ ์๋๊ฐ?
๋งจ ์์ ์๋๊ฒ ์์์ ์์๋ฅผ ๊ตฌ๋ณํ๋ ์ฉ๋๋ก ์ถ๊ฐํ ๋นํธ์ธ์ง ์๋์ง ์ด๋ป๊ฒ ์๊น?
๊ทธ๋ฌ๋ฉด ๋ ๊ทผ๋ณธ์ ์ธ ์ง๋ฌธ์ผ๋ก ๋์ด๊ฐ๊ฒ ๋๋ค.
10011010101 ์ด๋ ๊ฒ ์๋๊ฒ ์ ์ด์ ์ซ์์ธ๊ฑธ ์ฐ๋ฆฌ๋ ์ด๋ป๊ฒ ์๊น?
๊ฐ๋จํ ๋๋ตํ์๋ฉด ๊ทธ๋ด ํ์๊ฐ ์๋ค. ์ปดํ์ผ๋ฌ๊ฐ ํด๋น ์๋ฃ์ ์๋ฃํ์ ๋ฏธ๋ฆฌ ์ ํ๊ธฐ ๋๋ฌธ์.
๋๊ฐ์ 1110101์ด๋ผ๋ ๊ทธ๊ฒ int์ธ์ง string์ธ์ง ์๋๋ฉด ํน์ ๋ค๋ฅธ ๋ฌด์๊ฐ์ธ์ง์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ํด์๋๋ค๋๊ฒ์ด๋ค.
Two's complement representation
์ฌ๊ธฐ์๋ ์ ํํ ์์ฆ์ ์ด๋ป๊ฒ ์์๋ฅผ ํํํ๋์ง์ ๋ํด ์๋ ค์ค๋ค.
4๋นํธ ํํ๋ฒ์์ -8์ ๋ํ๋ด๋ณด์.
$n=4-1 , ZtS_{3}(โ8)=NtS_4(2^4โ8)=1000$
๊ทธ๋ผ 4๋นํธ์์ 16์ ์ด๋ป๊ฒ ๋ํ๋ผ๊น?
$NtS(16)=10000$ ์ธ๋ฐ ์ฌ๊ธฐ์ ์์ 4์๋ฆฌ๋ง ๊ฐ์ ธ์ค๋๊น 0000์ด๋ค.
๊ทธ๋์ 4๋นํธํํ๋ฒ ์์์ 1000์ 16์ด ์๋๋ผ -8์ด ๋๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก๋ ๊ณผ๊ฑฐ์ sign bit ๋ฐฉ์๊ณผ ๊ฐ๋ค. ์์ด 1์ด๋ฉด ์์ 0์ด๋ฉด ์์.
์ ๋ฆฌ์
-5/8 ์ด๋ผ๋ ์ ๋ฆฌ์๋ฅผ ์ด๋ป๊ฒ ํํํ ๊น.
-5 -> 1101, 8->01000 ํ๋ฉด (1101,01000) ๋ ์ด์ง์๊ฐ ๋์จ๋ค.
์ด๊ฑธ ๊ฐ ์ซ์๋ฅผ ๋๋ฒ์ฉ ๋ฐ๋ณตํ๋ค. 0์ 00์ผ๋ก 1์ 11๋ก.
11110011,0011000000 ์ด ๋๊ณ ๊ทธ ๋ ์ฌ์ด์ 01์ ๋ฃ๊ณ ์๋๋ค.
11110011 01 0011000000(๊ตฌ๋ณํ๊ธฐ ์ฝ๊ฒ ์คํ์ด์ค๋ฅผ ๋ฃ์) = -5/8
์ค์(๋ถ๋์์์ ํ๊ธฐ)
์ด๋ค ์ค์ x๊ฐ ์์ผ๋ฉด x์ ๊ทผ์ ํ $b * 2^e$ ๋ฅผ ์ฐพ์์ (b,e) ์์ผ๋ก ํํํ๋ค.
12.75 ์ ์ด๋ ๊ฒ ์ด์ง์๋ก ๋ณํ๋๋ค.
-
์ด์ง๋ฒ ๋ณํ
12๋ ์ด์ง์๋ก 1100
$0.75 = 0.5 + 0.25 = 1/2 + 1/2^2 = 0.11$ ๋์ ํฉ์ณ์1100.11
-
Scientific notation
1100.11์ด๋ ์ซ์๋ฅผ ์์ ํ ์๋ฆฌ๋ง ๋จ๊ธฐ๋๋ก ์์์ ์ ์ฎ๊ธฐ๋ฉด
1.10011์ด ๋๊ณ ์์์ ์ 3์๋ฆฌ๋ฅผ ์ฎ๊ฒผ์ผ๋ $1.10011 * 2^3$ ์ด๋ผ๊ณ ํ๊ธฐ. -
๊ฐ ๋ถ๋ถ ๊ตฌํ๊ธฐ
๋ถํธ: 12.75๋ ์์๋๊น sign bit์ 0.
์ง์: 3 + 127 = 130 = 10000010
๊ฐ์: 10011 (์์ 2๋จ๊ณ ์ซ์์์ ์์์ ๋ค ๋ถ๋ถ๋ง ๊ฐ์ ธ์ค๊ณ ๋๋จธ์ง๋ 0์ผ๋ก)
12.75 = 0 100000010 10011000000000000000000
12.3 ์ฒ๋ผ ๋ฑ ๋จ์ด์ง์ง ์๋ ์ค์๋?
12 -> 1100์ ๋์ผ.
0.3์ ์ด๋ป๊ฒ ๊ตฌํ๋?
0.3 ๋ฅผ 2์ฉ ๊ณฑํ๋ฉด์ ์์์ ์์ ์ซ์๋ฅผ ์ ๊ณ ๋ฒ๋ฆฐ๋ค.
0.3 * 2 = 0.6 -> 0
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
์ด๋ ๊ฒ ๋ฌดํํ ๊ฐ๋ค๊ฐ ์ ํด์ง ์๋ฆฟ์์์ ๋ฉ์ถ๋ค.(๋ณดํต์ 23๋นํธ. ์๋ํ๋ฉด float 32๋นํธ์์ ๋ถํธ 1๋นํธ, ์ง์ 8๋นํธ ๋ฅผ ๋นผ๋ฉด 23๋นํธ๋ง ๋จ๊ธฐ ๋๋ฌธ์)
๊ทธ๋์ 1100.01001.......0 ๊น์ง ๊ตฌํ๋ค์์ ๊ทธ๊ฑธ
์ ๊ทํ๋ฅผ ๊ฑฐ์ณ 1.10001001......0 * 2^3
๊ทธ๋์ 12.3 = 0 10000010 10001001100110011001101